package org.dromara.components.date;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.BaseEntity;

import java.util.Date;
import java.util.Map;

/**
 * @author: wangdm
 * @date 2024/03/02 14:25
 * @Description:
 */
@Slf4j
public class DateComponent {
    /**
     * 根据时间区间设计查询范围
     * @param baseEntity
     * @param timeRange
     */
    public static void setQueryTimeRange(String timeRange,BaseEntity baseEntity,String beginDateName,String endDateName){
        Date now = DateUtil.date();
        Map<String, Object> params = baseEntity.getParams();
        if(StringUtils.isEmpty(timeRange)){
            //timeRange为空时不处理
            return;
        }
        if(DateConstants.TIME_RANGE_TODAY.equalsIgnoreCase(timeRange)){
            //今日
            params.put(beginDateName,DateUtil.beginOfDay(now));
            params.put(endDateName,DateUtil.endOfDay(now));
        }else if(DateConstants.TIME_RANGE_THIS_WEEK.equalsIgnoreCase(timeRange)){
            //本周
            params.put(beginDateName,DateUtil.beginOfWeek(now));
            params.put(endDateName,DateUtil.endOfWeek(now));
        }else if(DateConstants.TIME_RANGE_THIS_MONTH.equalsIgnoreCase(timeRange)){
            //本月
            params.put(beginDateName,DateUtil.beginOfMonth(now));
            params.put(endDateName,DateUtil.endOfMonth(now));
        }else if(DateConstants.TIME_RANGE_BEFORE_MONTH.equalsIgnoreCase(timeRange)){
            //上月
            Date beginOfMonth = DateUtil.beginOfMonth(now);
            Date beforeMonth = DateUtil.offsetDay(beginOfMonth,-1);
            params.put(beginDateName,DateUtil.beginOfMonth(beforeMonth));
            params.put(endDateName,DateUtil.endOfMonth(beforeMonth));
        }
        else if(DateConstants.TIME_RANGE_THIS_YEAR.equalsIgnoreCase(timeRange)){
            //本年
            params.put(beginDateName,DateUtil.beginOfYear(now));
            params.put(endDateName,DateUtil.endOfYear(now));
        }else{
            //不支持的类型
            log.info("不支持的时间区间类型："+timeRange);
        }
    }
}
